﻿@inherits SsoCompontentBase
@inject IJSRuntime _js
@inject IPmClient _pmClient
@inject IAuthClient _authClient
@inject IConfiguration _configuration
@inject IDistributedCacheClient _distributedCacheClient
@inject IAuthenticationSchemeProvider _schemeProvider
@inject ProtectedLocalStorage _localStorage
@inject EnvironmentDataService _environmentData

<MForm Model="_inputModel" @ref="_loginForm" EnableI18n class="mt-12 mx-12 text-center">
    @if (_inputModel.PhoneLogin)
    {
        <MTextField PrependInnerIcon="mdi-cellphone" @bind-Value="_inputModel.PhoneNumber" Label="@T("PhoneNumber")"
                Placeholder="@T("PhoneNumberPlaceholder")" Outlined Autofocus>
    </MTextField>
        <CaptchaSendTextField Class="mt-3" @bind-Value="_inputModel.SmsCode" OnClick="GetSmsCode" />
    }
    else
    {
        <MTextField @bind-Value="_inputModel.Account" Label="@T("Account")" Placeholder="@T("AccountPlaceholder")"
                    Outlined Autofocus>
        <PrependInnerContent>
            <MIcon>mdi-account</MIcon>
        </PrependInnerContent>
    </MTextField>
        <MTextField @bind-Value="@_inputModel.Password" Class="mt-3" Label="@T("Password")" OnKeyDown="KeyDownHandler"
                Type="@(_showPwd ? "text" : "password")" Placeholder="@T("PasswordPlaceholder")"
                AppendIcon="@(_showPwd ? "mdi-eye" : "mdi-eye-off")" OnAppendClick="()=>_showPwd = !_showPwd" Outlined>
        <PrependInnerContent>
            <MIcon>mdi-lock</MIcon>
        </PrependInnerContent>
    </MTextField>
    }
    <MRow>
        <MCol Md="6">
            @if (_environments.Count() > 1)
            {
                <div class="fill-border d-flex rounded-lg pa-1">
                    <MIcon Size="22"
                       Color="@(_environments.FirstOrDefault(e=>e.Name == _inputModel.Environment)?.Color??"primary")">
                        mdi-record-circle
                    </MIcon>
                    <MSelect @bind-Value="Environment" Solo Dense HideDetails="true" Class="elevation-0" TextColor="neutral-lighten-2"
                         Items="@_environments" ItemText="u => u.Name" ItemValue="u => u.Name" MenuProps="@(props => { props.Bottom = true;props.OffsetY = true; })">
                    <SelectionContent Context="data">
                        <div style="width:100%" class="d-flex">
                            <span class="text-body2">@data.Item.Name</span>
                                <MDivider Class="fill--text ml-auto mr-2" Vertical></MDivider>
                            </div>
                        </SelectionContent>
                    </MSelect>
                </div>
            }
        </MCol>
        @if (!_inputModel.PhoneLogin)
        {
            <MCol Md="6" Class="d-flex justify-end">
                <MCheckbox @bind-Value="_inputModel.LdapLogin" Class="mt-2" HideDetails="true" Label="@T("LdapLogin")" TValue="bool"/>
            </MCol>
        }
    </MRow>
</MForm>
<div class="text-center mt-12">
    <MButton Class="rounded-pill text-btn white--text" Disabled=_loginLoading
             OnClick="LoginHandler" Height=48 Width=340 Color="primary" Loading=_loginLoading>
        @T("Login")
    </MButton>
    @if (!_inputModel.PhoneLogin)
    {
        <MButton Class="mt-6 rounded-pill text-btn white--text" OnClick="()=>{ _inputModel.PhoneLogin=true; }" Height=48
             Width=340 Color="primary" Outlined>
        @T("PhoneLogin")
    </MButton>
    }
    else
    {
        <MButton Class="mt-6 rounded-pill text-btn white--text" OnClick="()=>{ _inputModel.PhoneLogin=false; }" Height=48
             Width=340 Color="primary" Outlined>
        @T("PasswordLogin")
    </MButton>
    }
</div>

@if (ExternalProviderList.Any())
{
    <ExternalProviders ExternalProviderList="@ExternalProviderList" ReturnUrl="@_inputModel.ReturnUrl" />
}
